# -*- coding: utf-8 -*-
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops
from facility import logger


class HaoyangmaoSpider(RedisSpider):
    name = 'haoyangmao'

    def parse(self, response):
        yield Request(url='https://www.douban.com/group/565404/discussion?start=0', dont_filter=True,
                      callback=self.parse_item)

    def parse_item(self, response):
        logger(response.url, '\t', response.status)
        q = Ops(response)

        for i in range(1, q.get_num('//*[@id="content"]/div/div[1]/div[2]/table/tr')):
            yield {
                "id": q.get_str('//*[@id="content"]/div/div[1]/div[2]/table/tr[{}]/td[1]/a'.format(i + 1)).replace('😭',
                                                                                                                   ''),
                "link": q.get_attr('//*[@id="content"]/div/div[1]/div[2]/table/tr[{}]/td[1]/a'.format(i + 1), 'href')
            }
